#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Author: Bruno Alves 
Date: 2012/02/10
Version: v1.0
'''

import csv
import time
import sqlite3
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt 
import pylab
import locale


class Plot:

    
    def __init__(self):
        pass

    
    def PlotFile1(self, db_file):

        
        print 'Teachers by School and Year' 
	print 'Generating Graphic, wait a moment..'      
        timei = time.clock()

       
        conexao = sqlite3.connect(db_file)
        c = conexao.cursor()
        cmd = '''SELECT DISTINCT(tipo_estabelecimento) FROM docentes
                    ORDER BY tipo_estabelecimento'''
        c.execute(cmd)
        tipo_estab_list = c.fetchall()
        
        
        info_total = []

        for i in tipo_estab_list:
            data = []
            for year in range(2000,2011):
                
                estabelecimento = i[0].encode('utf-8')
                cmd = '''SELECT COUNT(nome) FROM docentes
                            WHERE tipo_estabelecimento == '{0}'
                            AND ano_lectivo == {1}'''.format(estabelecimento, year)
                c.execute(cmd)
                information = c.fetchall()

               
                for x in information:
                    data.append(x[0])
                    pass                
                pass

            
            info_total.append(data)         
            pass

        conexao.commit()
        conexao.close()

        ident = 0
        
        for valor in info_total:
            label_label = str(tipo_estab_list[ident])
            plt.plot(valor, linewidth=1.0, marker='o', label=label_label)
            ident += 1
            pass
        
        
        pylab.legend(loc="upper right", mode="expand", bbox_to_anchor=(1, 1))
        pylab.savefig('GraphTSA.png')        
        plt.ylabel('Values for')
        plt.xlabel('Year 2000 until 2010')
        plt.grid(True)
        plt.title('Teachers in a Scool per Year')
        plt.show()

        timef = time.clock()
        print 'Time Elapsed', timef-timei
        print 'Finished'
            
          
    def PlotFile2(self, estabelecimento, db_file):
      
        print 'Teachers by Categorie, School and Year' 
        print 'Generating Graphic, wait a moment..'
        timei = time.clock()

        
        conexao = sqlite3.connect(db_file)
        c = conexao.cursor()
        cmd = '''SELECT DISTINCT(categoria) FROM docentes
                    WHERE nome_estabelecimento == "{0}"'''.format(estabelecimento)
        c.execute(cmd)
        tipo_categoria_list = c.fetchall()
        
        
        info_total = []

        for i in tipo_categoria_list:
            
            data = []
            for year in range(2000,2011):
                categoria = i[0].encode('utf-8')
                cmd = '''SELECT COUNT(nome) FROM docentes
                            WHERE nome_estabelecimento == '{0}'
                            AND categoria == '{1}'
                            AND ano_lectivo == {2}'''.format(categoria, year)
                c.execute(cmd)
                information = c.fetchall()

                
                for x in information:
                    data.append(x[0])
                    pass                
                pass

            
            info_total.append(data)
        
        conexao.commit()
        conexao.close()

        ident = 0
        
        for valor in info_total:
            label_label = str(tipo_categoria_list[ident])
            plt.plot(valor, linewidth=1.0, marker='o', label=label_label)
            ident += 1
            pass
        
       
        pylab.legend(loc="upper right", mode="expand", bbox_to_anchor=(1, 1))
        pylab.savefig('Graphic2.png')        
        plt.ylabel('Values for')
        plt.xlabel('Year 2000 until 2010')
        plt.grid(True)
        plt.title('Teachers by Categorie, School and Year')
        plt.show()

        timef = time.clock()
        print 'Time Elapsed', timef-timei
        print 'Finished'
        
   
    def PlotFile3(self, estabelecimento, db_file):

        print 'Teachers by Level, School and Year'
        print 'Generating Graphic, wait a moment..'
        timei = time.clock()

        
        conexao = sqlite3.connect(db_file)
        c = conexao.cursor()
        cmd = '''SELECT DISTINCT(ultimo_grau) FROM docentes
                    WHERE tipo_estabelecimento =="{0}"'''.format(estabelecimento)
        c.execute(cmd)
        tipo_grau_list = c.fetchall()
        
        
        info_total = []

        
        z = 0
        try:
            for i in tipo_grau_list:
                
                data = []
                for year in range(2000,2011):
                    
                    grau = i[0].encode('utf-8')
                    cmd = '''SELECT COUNT(nome) FROM docentes
                                WHERE ultimo_grau == '{0}'
                                AND ano_lectivo == {1}'''.format(grau, year)
                    c.execute(cmd)
                    information = c.fetchall()
                    

                    
                    for x in information:
                        data.append(x[0])
                        pass                
                    pass

                
                info_total.append(data)         
                pass
        except:
            pass

        conexao.commit()
        conexao.close()

        ident = 0
        
        for valor in info_total:
            label_label = str(tipo_grau_list[ident])
            plt.plot(valor, linewidth=1.0, marker='o', label=label_label)
            ident += 1
            pass
        
        
        pylab.legend(loc="upper right", mode="expand", bbox_to_anchor=(1, 1))
        pylab.savefig('Graphic3.png')        
        plt.ylabel('Values for')
        plt.xlabel('Year 2000 until 2010')
        plt.grid(True)
        plt.title('All Teachers by Level and Year')
        plt.show()

        timef = time.clock()
        
        print 'Elapsed Time =', timef-timei
        print 'Finished'

    def PlotHTML(self, data, data_label):

        print 'Html Total Tags'
        print 'Generating Graphic, wait a moment...'
        timei = time.clock()
        plt.plot(data, linewidth=1.0, marker='o', label=str(data_label))
        plt.xticks(range(len(data_label)), data_label)

        pylab.savefig('Graphic_4.png')        
        plt.ylabel('Values for')
        plt.xlabel('HTML Tags')
        plt.grid(True)
        plt.title('HTML Tags per WebPage')
        plt.show()

        timef = time.clock()

        print 'Elapsed Time =', timef-timei
        print 'Finished'



